<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *模板管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'弹窗列表'),
	array('op'=>'edit','title'=>'添加弹窗'),
);
set_html('tabs',$tabs);
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_weishop_activity_tan',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
    $aitip='每个弹窗，可以选择不同的页面，不同的会员等级弹窗显示！';
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>$aitip,'edit'=>true,'list'=>array(
			array('name'=>'title','title'=>'标题','type'=>'string'),
            array('type'=>'image','title'=>'弹窗图','name'=>'image'),
            array('type'=>'radio','title'=>'弹窗权限','name'=>'showtype','options'=>'showtypes'),
            array('name'=>'start_time','title'=>'开始时间','type'=>'time'),
            array('name'=>'end_time','title'=>'结束时间','type'=>'time'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'标题','type'=>'string'),
		)
	);
    $return['showtypes']=array('不限制','指定会员等级弹窗','指定会员组弹窗');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_weishop_activity_tan',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['links'] =json_decode($item['links'],true);
	}else{
		$item=array();
	}
    $item['start_time'] =$item['start_time']?date('Y/m/d H:i',$item['start_time']):'';
    $item['end_time'] =$item['end_time']?date('Y/m/d H:i',$item['end_time']):'';
	$return['item']=$item;
	$htmls[]=array(
		'type'=>'look',
		'width'=>'350px',
		'flex'=>1,
		'look'=>array(
				array('type'=>'imglinks','name'=>'links'),
        ),
	);
	$htmls[]=array(
		'type'=>'edit',
		'post'=>'post',
		'edit'=>array(
		    array('type'=>'string','title'=>'弹窗名称','name'=>'title'),
            array('type'=>'image','title'=>'弹窗图','name'=>'image'),
            array('type'=>'string','title'=>'弹窗说明','name'=>'desc'),
            array('type'=>'checkbox','title'=>'弹窗页面','name'=>'pagetype','options'=>'pagetypes'),
			array('type'=>'unitstring','unit'=>'分钟','title'=>'弹窗间隔时间','name'=>'showtime','help'=>'不填，则按系统缓存时间间隔，弹窗间隔时间不能大于系统缓存时间！'),
            array('type'=>'radio','title'=>'弹窗权限','name'=>'showtype','options'=>'showtypes'),
            array('name'=>'levels','title'=>'指定会员等级弹窗','type'=>'checkbox','options'=>'levels','show'=>array('showtype'=>1)),
            array('name'=>'groups','title'=>'指定会员组弹窗','type'=>'checkbox','options'=>'groups','show'=>array('showtype'=>2)),
            array('type'=>'link','from'=>'action','title'=>'主链接','name'=>'url'),
            array(
                'type'=>'array',
                'name'=>'links',
                'title'=>'图片热点链接',
                'add_tip'=>'添加自定义表单项',
								'list'=>array(
                                    array('type'=>'string','title'=>'链接地址','name'=>'url'),
									array('type'=>'unitstring','unit'=>'px','name'=>'left','title'=>'左边距'),
                                    array('type'=>'unitstring','unit'=>'px','name'=>'top','title'=>'顶部距'),
                                    array('type'=>'unitstring','unit'=>'px','name'=>'width','title'=>'宽'),
                                    array('type'=>'unitstring','unit'=>'px','name'=>'height','title'=>'高'),
								),
								'details'=>array(
                                    array('type'=>'link','from'=>'action','title'=>'链接地址','name'=>'url'),
                                    array('type'=>'unitstring','unit'=>'px','name'=>'left','title'=>'左边距'),
                                    array('type'=>'unitstring','unit'=>'px','name'=>'top','title'=>'顶部距'),
                                    array('type'=>'unitstring','unit'=>'px','name'=>'width','title'=>'宽'),
                                    array('type'=>'unitstring','unit'=>'px','name'=>'height','title'=>'高'),
								)
            ),
            array('name'=>'start_time','title'=>'开始时间','type'=>'time'),
            array('name'=>'end_time','title'=>'结束时间','type'=>'time'),
            array('type'=>'switch','title'=>'状态','name'=>'status'),
		)
	);
	$return['pagetypes']=array('首页','列表页','商品页','订单列表页','会员中心');
    $return['showtypes']=array('不限制','指定会员等级弹窗','指定会员组弹窗');
    $levels=pdo_getall('jy_weishop_member_level',array('merchid'=>0,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels']=array(0=>'默认等级');
    foreach($levels as $lv){
        $return['levels'][$lv['id']]=$lv['title'];
    }
    $groups=pdo_getall('jy_weishop_member_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
    $return['groups']=array(0=>'默认分组');
    foreach($groups as $lv){
        $return['groups'][$lv['id']]=$lv['title'];
    }
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
        'desc'=>trim($postdata['desc']),
        'image'=>trim($postdata['image']),
        'start_time'=>strtotime($postdata['start_time']),
        'end_time'=>strtotime($postdata['end_time']),
        'pagetype'=>trim($postdata['pagetype']),
        'levels'=>trim($postdata['levels']),
        'groups'=>trim($postdata['groups']),
		'showtype'=>intval($postdata['showtype']),
        'url'=>trim($postdata['url']),
		'links'=>ijson_encode($postdata['links']),
		'status'=>intval($postdata['status']),
        'showtime'=>intval($postdata['showtime']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_weishop_activity_tan',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
        $data['createtime']=time();
		pdo_insert('jy_weishop_activity_tan',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_weishop_activity_tan',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}